//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    bnrUrl: null,
    classlist: null,
    moduleList: null,
    cookbookPermit: null,
    schoolid:null,
    schoollist: null,
    letter:'3'
  },

  mokuaibuttton: function (e) {
    var index = e.currentTarget.dataset.index;
    var moduleCode = this.data.moduleList[index].ModuleCode
    console.log(moduleCode)
    if (moduleCode == 'new') {
      wx.navigateTo({
        url: '../news/news'
      })
    } else if (moduleCode == 'homework') {
      wx.navigateTo({
        url: '../homework/homework'
      })
    } else if (moduleCode == 'notify') {
      wx.navigateTo({
        url: '../notify/notify'
      })
    } else if (moduleCode == 'cookbook') {
      wx.navigateTo({
        url: '../cookbook/cookbook'
      })
    } else if (moduleCode == 'teacherinfo') {
      wx.navigateTo({
        url: '../teacher/teacher'
      })
    } else if (moduleCode == 'health') {
      wx.navigateTo({
        url: '../childhealth/childhealth'
      })
    } else if (moduleCode == 'studentbook') {
      wx.navigateTo({
        url: '../weekplan/weekplan'
      })
    } else if (moduleCode == 'movie') {
      wx.navigateTo({
        url: '../childonline/childonline'
      })
    } else if (moduleCode == 'mail') {
      wx.navigateTo({
        url: '../yuanzhangemail/yuanzhangemail'
      })
    } else if (moduleCode == 'archives') {
      wx.navigateTo({
        url: '../babymessage/babymessage'
      })
    } else if (moduleCode == 'teacherattendance') {
      wx.navigateTo({
        url: '../teacherattendance/teacherattendance'
      })
    } else if (moduleCode == 'mealreports') {
      wx.navigateTo({
        url: '../jiucanrenshu/jiucanrenshu'
      })
    } else if (moduleCode == 'meal') {
      wx.navigateTo({
        url: '../jiucanhuibao/jiucanhuibao'
      })
    } else if (moduleCode == 'kidattendance') {
      wx.navigateTo({
        url: '../childattendanceone/childattendanceone'
      })
    } else if (moduleCode == 'classorder') {
      wx.navigateTo({
        url: '../classsort/classsort'
      })
    } else if (moduleCode == 'drug') {
      wx.navigateTo({
        url: '../fuyaoguanli/fuyaoguanli'
      })
    }else {
      wx.showModal({
        title: '提示',
        content: '此模块暂未开放',
      })
    }

  },
  onLoad: function () {
    
  },
  onShow: function () {
    var list = wx.getStorageSync('schoollist');
    var schoollist=[]
    for (var i = 0; i < list.length; i++) {
      var schoolobj = {
        text: list[i].schoolName,
        value: list[i].schoolID
      }
      schoollist.push(schoolobj)
    }
    this.setData({
      schoolid:list[0].schoolID,
      schoollist: schoollist
    })
    if (list.length > 0) {
      app.globalData.schoolId = list[0].schoolID
      app.globalData.schoolName = list[0].schoolName
    }
    console.log("userId==" + app.globalData.userId)
    console.log("schoolId==" + app.globalData.schoolId)
    console.log("classid==" + app.globalData.classid)
    console.log("classname==" + app.globalData.classname)
    this.getData();
  },
  onschoolchange({
    detail
  }) {
    this.setData({
      schoolid: detail
    });
   
    app.globalData.schoolId = detail
    this.getData();
  },

  /*点击变色*/
  click: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this
    that.setData({
      id: id
    })
  },



  getData: function () {
    var that = this;
    var mobile = wx.getStorageSync('mobile'); //存储用户名
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "schoolRole": app.globalData.schoolRole,
      "mobileType": app.globalData.mobileType,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobile": mobile,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_homepage"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {


          var list = res.data.data.moduleList;
          var list1 = [];
          list.forEach(function (item, index) {
            console.log(item); //这里的item就是从数组里拿出来的每一个每一组
            var moduleCode = item.ModuleCode
            if (moduleCode == 'new') {
              list[index].ModuleKidgardenUrl = "../../images/icon_news.png"
              list1.push(item);
            } else if (moduleCode == 'homework') {
              list[index].ModuleKidgardenUrl = "../../images/icon_homework.png"
              list1.push(item);
            } else if (moduleCode == 'notify') {
              list[index].ModuleKidgardenUrl = "../../images/icon_xiaoyuantongzhi.png"
              list1.push(item);
            } else if (moduleCode == 'archives') {
              list[index].ModuleKidgardenUrl = "../../images/icon_document.png"
              list1.push(item);
            } else if (moduleCode == 'cookbook') {
              list[index].ModuleKidgardenUrl = "../../images/icon_meizhoushipu.png"
              list1.push(item);
            } else if (moduleCode == 'teacherattendance') {
              list[index].ModuleKidgardenUrl = "../../images/icon_teacher_attendance.png"
              list1.push(item);
            } else if (moduleCode == 'meal') {
              list[index].ModuleKidgardenUrl = "../../images/icon_eating.png"
              list1.push(item);
            } else if (moduleCode == 'mail') {
              list[index].ModuleKidgardenUrl = "../../images/icon_email.png"
              list1.push(item);
            } else if (moduleCode == 'kidattendance') {
              list[index].ModuleKidgardenUrl = "../../images/icon_kaoqinjiesong.png"
              list1.push(item);
            } else if (moduleCode == 'studentbook') {
              list[index].ModuleKidgardenUrl = "../../images/icon_week_plan.png"
              list1.push(item);
            } else if (moduleCode == 'movie') {
              list[index].ModuleKidgardenUrl = "../../images/icon_babyonline.png"
              list1.push(item);
            } else if (moduleCode == 'classorder') {
              list[index].ModuleKidgardenUrl = "../../images/icon_paixu.png"
              list1.push(item);
            } else if (moduleCode == 'mealreports') {
              list[index].ModuleKidgardenUrl = "../../images/icon_jiucanrenshu.png"
              list1.push(item);
            }else if (moduleCode == 'drug') {
              list[index].ModuleKidgardenUrl = "../../images/icon_mediceine.png"
              list1.push(item);
            }
          })
          that.setData({
            bnrUrl: res.data.data.adList,
            moduleList: list1,
            classlist: res.data.data.classlist,
            cookbookPermit: res.data.data.cookbookPermit
          })
          wx.setStorageSync('classlist', res.data.data.classlist);
          wx.setStorageSync('cookbookPermit', res.data.data.cookbookPermit);
        } else {
          wx.showToast({
            title: "获取信息失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        console.log('submit fail');
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })

  },

})